
class WNode {
    constructor(val) {
        this.val = val;
        this.next = null; // 链表next
    }
}
function create(list) {
    let node1 = new WNode(list[0]);
    let p = node1;
    for (let i = 1; i < list.length; i++) {
        p.next = new WNode(list[i]);
        p = p.next;
    }
    return node1;
}
/**
 * 链表两数相加
 * @param {*} node1 
 * @param {*} node2 
 */
function sumLink(node1, node2) {
    let p1 = node1;
    let p2 = node2;
    let prev = new WNode(-1);
    // let vit = new WNode(999,);
    let header = prev;
    let factor = 0;
    while (p1 || p2) {
        let num1 = 0;
        let num2 = 0;
        let tmp = 0;
        if (p1) {
            num1 = p1.val;
        }
        if (p2) {
            num2 = p2.val;
        }
        if (factor + num1 + num2 > 10) {
            tmp = factor + num1 + num2 - 10;
            factor = 1;
        } else {
            tmp = num1 + num2;
            factor = 0;
        }

        prev.next = new WNode(tmp);
        prev = prev.next;
        p1 = p1.next;
        p2 = p2.next;
    }
    console.log(prev)
    // 最后看下factor是否还有进位数，
    // 有就继续创建新的节点接受
    if (factor) {
        prev.next = new WNode(factor);
        prev = prev.next;
    }
    return header.next;
    // console.log(JSON.stringify(header, undefined, 4));
}

// 通过数组创建两个整数链表
let node1 = create([9, 8, 6]);
let node2 = create([9, 9, 8]);

console.log("[相加链表]", sumLink(node1, node2));
